package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与文章相关的业务
 */
@Controller
public class ArticleController {

    public static final String ARTICLE_DIR = "./articles/";
    static {
        File dir = new File(ARTICLE_DIR);
        if(!dir.exists()) {//如果该目录不存在则自动创建
            dir.mkdirs();
        }
    }
    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章....");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if(title==null||author==null||content==null){
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        try (
                FileOutputStream fos = new FileOutputStream(ARTICLE_DIR+title+".obj");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            Article article = new Article(title,author,content);
            oos.writeObject(article);
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("发表文章处理完毕!");
    }
    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理动态页面...");
        List<Article> articleList = new ArrayList<>();
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".obj"));
        for (File articleFile: subs){
            try(
                    FileInputStream fis = new FileInputStream(articleFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
              ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList);
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article: articleList) {
            pw.println("<tr>");
            pw.println("<td><a href=\"/myweb/showArticle?title="+article.getTitle()+"\">"+article.getTitle()+"</a></td>");
            pw.println("<td>"+article.getAuthor()+"</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        System.out.println("动态页面处理完毕!");
    }
    @RequestMapping("/myweb/showArticle")
    public void showArticle(HttpServletRequest request,HttpServletResponse response){
        List<Article> articleList = new ArrayList<>();
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".obj"));
        for (File articleFile: subs){
            try(
                    FileInputStream fis = new FileInputStream(articleFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList);
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        for (Article article : articleList) {
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>"+article.getTitle()+"</td>");
            pw.println("</tr>");

            pw.println("<tr>");
            pw.println("<td>作者</td>");
            pw.println("<td>"+article.getAuthor()+"</td>");
            pw.println("</tr>");

            pw.println("<tr>");
            pw.println("<td colspan=\"2\">"+article.getContent()+"</td>");
            pw.println("</tr>");
        }

        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
    }

}
